﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace YJKWallDesignation
{
	public partial class SuffixMarkSettingForm : System.Windows.Forms.Form
	{
		public SuffixMarkSettingForm()
		{
			this.InitializeComponent();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			List<string> currentSuffixMarkList = this.GetCurrentSuffixMarkList();
			SetSuffixNameForm setSuffixNameForm = new SetSuffixNameForm("新后缀分隔符", currentSuffixMarkList);
			if (setSuffixNameForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			this.listBoxSuffixList.Items.Add(setSuffixNameForm.SuffixName);
		}

		private void btnRename_Click(object sender, EventArgs e)
		{
			int selectedIndex = this.listBoxSuffixList.SelectedIndex;
			if (selectedIndex == -1)
			{
				return;
			}
			string oldName = this.listBoxSuffixList.Items[selectedIndex].ToString();
			List<string> currentSuffixMarkList = this.GetCurrentSuffixMarkList();
			SetSuffixNameForm setSuffixNameForm = new SetSuffixNameForm(oldName, currentSuffixMarkList);
			if (setSuffixNameForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			this.listBoxSuffixList.Items[selectedIndex] = setSuffixNameForm.SuffixName;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (this.listBoxSuffixList.SelectedIndex == -1)
			{
				return;
			}
			this.listBoxSuffixList.Items.RemoveAt(this.listBoxSuffixList.SelectedIndex);
		}

		private void SuffixSettingForm_Load(object sender, EventArgs e)
		{
			foreach (string item in SuffixMarkSettingForm.ReadSuffixMarkList())
			{
				this.listBoxSuffixList.Items.Add(item);
			}
		}

		private void SuffixSettingForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			List<string> currentSuffixMarkList = this.GetCurrentSuffixMarkList();
			this.WriteSuffixMarkList(currentSuffixMarkList);
		}

		private List<string> GetCurrentSuffixMarkList()
		{
			List<string> list = new List<string>();
			foreach (object obj in this.listBoxSuffixList.Items)
			{
				list.Add(obj.ToString());
			}
			return list;
		}

		public static List<string> ReadSuffixMarkList()
		{
			List<string> list = new List<string>();
			string path = Path.GetTempPath() + "SuffixMarkSettingForm.dat";
			if (!File.Exists(path))
			{
				return list;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int num = Convert.ToInt32(streamReader.ReadLine());
			for (int i = 0; i < num; i++)
			{
				list.Add(streamReader.ReadLine());
			}
			streamReader.Close();
			fileStream.Close();
			return list;
		}

		private void WriteSuffixMarkList(List<string> suffixList)
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SuffixMarkSettingForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(suffixList.Count);
			foreach (string value in suffixList)
			{
				streamWriter.WriteLine(value);
			}
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}
	}
}
